using B.WMS.Good;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;

namespace B.WMS.HttpApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OSSController : AbpController
    {
        private readonly OSSService _ossService;

        public OSSController(OSSService ossService)
        {
            _ossService = ossService;
        }

        /// <summary>
        /// 上传单个文件
        /// </summary>
        /// <param name="file">文件</param>
        /// <returns>文件URL</returns>
        [HttpPost("upload")]
        public async Task<IActionResult> Upload(IFormFile file)
        {
            try
            {
                if (file == null || file.Length == 0)
                {
                    return BadRequest("请选择要上传的文件");
                }

                var url = await _ossService.UploadFileAsync(file, "test");
                return Ok(new { success = true, url = url, message = "上传成功" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = $"上传失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 批量上传文件
        /// </summary>
        /// <param name="files">文件列表</param>
        /// <returns>上传结果</returns>
        [HttpPost("upload-multiple")]
        public async Task<IActionResult> UploadMultiple(IFormFileCollection files)
        {
            try
            {
                if (files == null || files.Count == 0)
                {
                    return BadRequest("请选择要上传的文件");
                }

                var results = await _ossService.UploadMultipleFilesAsync(files, "test");
                return Ok(new { success = true, results = results });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = $"批量上传失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileUrl">文件URL</param>
        /// <returns>删除结果</returns>
        [HttpDelete("delete")]
        public async Task<IActionResult> DeleteFile([FromQuery] string fileUrl)
        {
            try
            {
                if (string.IsNullOrEmpty(fileUrl))
                {
                    return BadRequest("请提供文件URL");
                }

                var result = await _ossService.DeleteFileAsync(fileUrl);
                return Ok(new { success = result, message = result ? "删除成功" : "删除失败" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = $"删除失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 获取文件信息
        /// </summary>
        /// <param name="fileUrl">文件URL</param>
        /// <returns>文件信息</returns>
        [HttpGet("file-info")]
        public async Task<IActionResult> GetFileInfo([FromQuery] string fileUrl)
        {
            try
            {
                if (string.IsNullOrEmpty(fileUrl))
                {
                    return BadRequest("请提供文件URL");
                }

                var fileInfo = await _ossService.GetFileInfoAsync(fileUrl);
                return Ok(new { success = true, fileInfo = fileInfo });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = $"获取文件信息失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 检查Bucket状态
        /// </summary>
        /// <returns>Bucket状态</returns>
        [HttpGet("bucket-status")]
        public IActionResult GetBucketStatus()
        {
            try
            {
                var exists = _ossService.BucketExists();
                return Ok(new { success = true, bucketExists = exists, message = exists ? "Bucket存在" : "Bucket不存在" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = $"检查Bucket状态失败：{ex.Message}" });
            }
        }
    }
} 